package cn.hwua.web;

import cn.hwua.dao.NewsDao;
import cn.hwua.dao.impl.NewsDaoImpl;
import cn.hwua.entity.Company;
import cn.hwua.entity.News;
import cn.hwua.util.UUIDUtils;
import cn.hwua.util.Upload;
import com.alibaba.fastjson.JSON;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet("/news/*")
public class NewsServlet extends HttpServlet {
    private NewsDao dao = new NewsDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String baseURL = req.getContextPath();//获取项目的基本路径
        String uri = req.getRequestURI();
        String path = uri.substring(uri.lastIndexOf("/") + 1);
        if ("list.do".equals(path)) {
            list(req, resp, baseURL);
            return;
        } else if ("news-con.do".equals(path)) {
            news_con(req, resp, baseURL);
            return;
        } else if ("news-read.do".equals(path)) {
            news_read(req, resp);
            return;

        } else if ("admin_list.do".equals(path)) {
            admin_list(req, resp, baseURL);
            return;
        } else if ("admin_delete.do".equals(path)) {
            admin_delete(req, resp);
        } else if ("admin_add.do".equals(path)) {
            admin_add(req, resp, baseURL);
            return;
        }else if ("admin_findid.do".equals(path)) {
            resp.setContentType("application/json;charset=utf-8");
            String id = req.getParameter("id");
            News news = dao.findById(id);
            req.setAttribute("news",news);
            req.getRequestDispatcher(baseURL + "/admin/news_update.jsp").forward(req, resp);
            return;
        }else if ("admin_update.do".equals(path)) {
            resp.setContentType("application/json;charset=utf-8");
            String news_title = req.getParameter("news_title");
            String news_author = req.getParameter("news_author");
            String news_readnums = req.getParameter("news_readnums");
            String news_img = req.getParameter("news_img");
            String news_content = req.getParameter("news_content");
            String news_addtime = req.getParameter("news_addtime");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            ParsePosition pos = new ParsePosition(0);
            Date date = formatter.parse(news_addtime, pos);
            String news_id = req.getParameter("news_id");
            News news = new News();
            news.setNews_id(Integer.parseInt(news_id));
            news.setNews_addtime(date);
            news.setNews_author(news_author);
            news.setNews_content(news_content);
            news.setNews_img(news_img);
            news.setNews_readnums(Integer.parseInt(news_readnums));
            news.setNews_title(news_title);
            int save = dao.update(news);
            resp.sendRedirect(baseURL + "/news/admin_list.do");
            return;
        } else {
            throw new RuntimeException("请求错误");
        }
    }

    private void admin_add(HttpServletRequest req, HttpServletResponse resp, String baseURL) throws IOException {
        resp.setContentType("application/json;charset=utf-8");
        String news_title = req.getParameter("news_title");
        String news_author = req.getParameter("news_author");
        String news_readnums = req.getParameter("news_readnums");
        String news_img = req.getParameter("news_img");
        String news_content = req.getParameter("news_content");
        String news_addtime = req.getParameter("news_addtime");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(news_addtime, pos);
        String news_id = req.getParameter("news_id");
        News news = new News();
        news.setNews_id(Integer.parseInt(news_id));
        news.setNews_addtime(date);
        news.setNews_author(news_author);
        news.setNews_content(news_content);
        news.setNews_img(news_img);
        news.setNews_readnums(Integer.parseInt(news_readnums));
        news.setNews_title(news_title);
        int save = dao.save(news);
        resp.sendRedirect(baseURL + "/news/admin_list.do");
        return;
    }

    private void admin_delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        News n = dao.findById(id);
        int delete = dao.delete(n);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.write("删除成功");
        out.close();
    }

    private void admin_list(HttpServletRequest req, HttpServletResponse resp, String baseURL) throws ServletException, IOException {
        //后台新闻列表页
        int pageNum = 1;
        int pageSize = 10;
        String p = req.getParameter("p");//获取页码
        String pagesize = req.getParameter("pagesize");
        pageNum = null != p ? Integer.parseInt(p) : 1;
        pageSize = null != pagesize ? Integer.parseInt(pagesize) : 10;
        //总记录数
        long totalNums = dao.count();
        long totalPage = (totalNums % pageSize == 0) ? (totalNums / pageSize) : (totalNums / pageSize + 1);


        List<News> newsList = dao.findPage(pageNum, pageSize);
        req.setAttribute("currentPage", pageNum);
        req.setAttribute("totalPage", totalPage);
        req.setAttribute("pageSize", pageSize);
        req.setAttribute("newsList", newsList);
        req.setAttribute("pathURL", baseURL + "/news/admin_list.do");
        req.getRequestDispatcher(baseURL + "/admin/news_list.jsp").forward(req, resp);
        return;
    }

    private void news_con(HttpServletRequest req, HttpServletResponse resp, String baseURL) throws ServletException, IOException {
        String newsId = req.getParameter("newsId");
        News news = dao.findById(newsId);
        req.setAttribute("news", news);
        req.getRequestDispatcher(baseURL + "/pc/news-con.jsp").forward(req, resp);
        return;
    }

    private void news_read(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int news_id = Integer.parseInt(req.getParameter("news_id"));
        int nums = dao.increaseReadNums(news_id);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.write("success");
        out.close();
        return;
    }

    private void list(HttpServletRequest req, HttpServletResponse resp, String baseURL) throws ServletException, IOException {
        int pageNum = 1;
        int pageSize = 10;
        String p = req.getParameter("p");//获取页码
        String pagesize = req.getParameter("pagesize");
        pageNum = null != p ? Integer.parseInt(p) : 1;
        pageSize = null != pagesize ? Integer.parseInt(pagesize) : 10;
        //总记录数
        long totalNums = dao.count();
        long totalPage = (totalNums % pageSize == 0) ? (totalNums / pageSize) : (totalNums / pageSize + 1);


        List<News> newsList = dao.findPage(pageNum, pageSize);
        req.setAttribute("currentPage", pageNum);
        req.setAttribute("totalPage", totalPage);
        req.setAttribute("pageSize", pageSize);
        req.setAttribute("newsList", newsList);
        req.setAttribute("pathURL", baseURL + "/news/list.do");
        req.setAttribute("newsList", newsList);
        req.getRequestDispatcher(baseURL + "/pc/news.jsp").forward(req, resp);
        return;
    }
}
